home *** CD-ROM | disk | FTP | other *** search
- #!/usr2/local/bin/perl -w
-
- use GD;
- require "cgilib.pl";
-
-
- sub centerString
- {
- my ($img, # gd Image to render in
- $font, # gd Font to render with
- $x, $y, # x and y coordinates of center of string
- $txt, # text to render
- $colour) = @_; # colour to render the text in
-
- # Calculate the width of the text in pixels.
- my $width = length($txt)*$font->width;
-
- $img->string($font,1+$x-$width/2,$y,$txt,$colour);
- }
-
-
- # Output the GIF content type
- print "Content-type: image/gif\n\n";
-
- # Initialize a hash of CGI arguments
- # using routines from cgilib.pl
- readParse(*dict);
-
- my $barWidth = $dict{'barWidth'} || 20;
- my $barHeight = $dict{'barHeight'} || 100;
- my $barPadding = $dict{'barPadding'} || 2;
- my @score = split / /,($dict{'score'} || '1 2 3 4 5 6 7 8 9 10');
-
- my $topPadding = 15;
- my $bottomPadding = 20;
-
- my $xAxis = $topPadding+$barHeight;
- my $width = 10*$barWidth+11*$barPadding;
- my $height = $barHeight+$topPadding+$bottomPadding;
-
- # Create a blank gd image of the required size
- my $im = new GD::Image($width,$height);
-
- # Allocate colors
- my $black = $im->colorAllocate( 0, 0, 0);
- my $white = $im->colorAllocate(255,255,255);
- my $red = $im->colorAllocate(255,0,0);
-
- # Make the image interlaced
- $im->interlaced('true');
-
- # Draw the X-axis
- $im->line(0,$xAxis,$width-1,$xAxis,$white);
-
- # Determine the height of the highest bar
- for ( $max=0, $i=0 ; $i<10 ; $i++ )
- {
- $max = $score[$i] if ($score[$i] > $max);
- }
-
- # For each of the 10 bars
- for ( $i=0 ; $i<10 ; $i++)
- {
- # Calculate the X coordinate for the center of the bar
- $x = ($i+0.5)*$barWidth+($i+1)*$barPadding;
-
- # calculate how high this bar should be
- $barLen = $score[$i]*$barHeight/$max;
-
- # draw a tick mark on the x-axis
- $im->line($x,$xAxis+1,$x,$xAxis+2,$white);
-
- # label the x-axis with numbers from 1 to 10
- # using the bold font
- centerString($im,
- gdMediumBoldFont,
- $x, $xAxis+3, # X, Y coordinates
- $i+1, # text to display
- $white);
-
- # label the height of the bar using the small font
- centerString($im,
- gdSmallFont,
- $x, $xAxis-$barLen-11,
- $score[$i],
- $white);
-
- # draw one bar
- $im->filledRectangle(
- $x-$barWidth/2, $xAxis-$barLen,
- $x+$barWidth/2, $xAxis-1,
- $red);
- }
-
- # Convert the image to GIF and print it to standard output
- print $im->gif;
-
- 1;
-